﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.Profile;
using TenderSampleApp.Models;
using TenderSampleApp.Models.Profile;
using TenderSampleApp.Code;

namespace TenderSampleApp.Areas.RAC.Controllers
{
    public class RACRequestController : Controller
    {
        [Authorize(Roles = "RAC")]
        public ActionResult ChoseTypeOfRequest(string typeOfPerson, int id = 0)
        {
            DAL dl = new DAL();
            string code = id != 0 ? dl.GetVATCode(id) : null;
            if (typeOfPerson == "1")
            {
                return RedirectToAction("GetPersonRequest", new { code = code, area="RAC" });
            }
            else if (typeOfPerson == "2")
            {
                return RedirectToAction("GetJuristRequest", new { code = code, area = "RAC" });
            }
            else
            {
                 ViewBag.DDList = new List<SelectListItem>(){new SelectListItem(){ Text="Фізична особа",Value="1"}, new SelectListItem(){ Text="Юридична особа",Value="2"}};
                return View();
            }
        }

        [Authorize(Roles = "RAC")]
        public ActionResult AcceptRequest(string requestTitle)
        {
            ViewData["requestTitle"] = requestTitle;
            return View();
        }

        [HttpGet]
        [Authorize(Roles = "RAC")]
        public ActionResult GetJuristRequest(string code)
        {
            JuristRequest pr = new JuristRequest() { VATCode = code };
            return View(pr);
        }

        [HttpPost]
        [Authorize(Roles = "RAC")]
        public ActionResult GetJuristRequest(JuristRequest jr)
        {
            if (ModelState.IsValid)
            {
                DAL dl = new DAL();
                int JuristicID = dl.AddJuridicalPerson(jr);
                dl.AddRepresentativeOfJuridicalPerson(jr, JuristicID);
                return RedirectToAction("AcceptRequest", new { requestTitle = "GetJuristRequest" });
            }
            return View(jr);
        }

        [HttpGet]
        [Authorize(Roles = "RAC")]
        public ActionResult GetPersonRequest(string code)
        {
            PersonRequest pr = new PersonRequest() { VATCode = code };
            return View(pr);
        }

        [HttpPost]
        [Authorize(Roles = "RAC")]
        public ActionResult GetPersonRequest(PersonRequest pr)
        {
            if (ModelState.IsValid)
            {
                DAL dl = new DAL();
                int PersonID = dl.AddPhysicalPerson(pr);
                if (!String.IsNullOrEmpty(pr.DocumentOfConfirm))
                    dl.AddRepresentativeOfPhysicalPerson(pr, PersonID);

                return RedirectToAction("AcceptRequest", new { requestTitle = "GetPersonRequest" });
            }
            return View(pr);
        }

    }
}
